from myapp.utils.mysqlM import db
# from django.http import HttpResponse
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
import time


class Showuser(APIView):
    def get(self, request):
        sql = "select * from customer"
        res = db.find_all(sql)
        return Response({'code': 200, 'list': res})

    def post(self, request):
        id = request.data.get('userid')  # 客户id
        status = request.data.get('status')  # 是否同意  1同意   2不同意
        # content = request.data.get('content')  # 审批意见

        audit_time = int(time.time())
        if status:
            if int(status) == 2:
                sql = "select * from customer where id={}".format(int(id))
                customer = db.find(sql)
                # 更新审批状态，审批时间，审批人
                sql1 = "update customer set status={},audit_time={},audit_name='zs' where id={}".format(int(status),
                                                                                                        audit_time,
                                                                                                        int(id))
                db.update(sql1)
                #查询注册来源
                sql2 = "select tcount from register where id={}".format(int(customer['refromid']))
                num = db.find(sql2)
                # 审批成功+1
                number = int(num['tcount']) + 1
                sql3 = "update register set tcount={} where id={}".format(int(number), int(customer['refromid']))
                db.update(sql3)
                # 更新客户经理 额度
                sql4 = "select tmoney from manager where id={}".format(int(customer['manager_id']))
                manager = db.find(sql4)
                money = int(customer['tmoney']) + int(manager['tmoney'])
                sql5="update manager set tmoney={} where id={}".format(int(money),int(customer['manager_id']))
                db.update(sql5)
            elif int(status) == 3:
                sql6="update customer set status=3 where id={}".format(int(id))
                db.update(sql6)
            return  Response({'code':200})
